package com.baike.controller;

import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbStudent;
import com.baike.mapper.TbStudentMapper;
import com.baike.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("loginController")
@RefreshScope
@Api(value = "学生登陆注册功能")
public class LoginController {

    @Autowired
    private TbStudentMapper tbStudentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

//    @RequestMapping("/login")
    @ApiOperation(value = "登陆业务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "query")
    })
    @GetMapping("login")
    public Result login(String username, String password, Model model){
//        1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
//        2.将账号密码封装成 token 对象
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//        3.调用subject 的login方法执行登陆逻辑
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            System.out.println("账号不存在...");
            model.addAttribute("message","账号不存在");
            return Result.build(ConstantEnum.UNKNOWN);
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
            model.addAttribute("message","密码错误");
            return Result.build(ConstantEnum.INCORRECT);
        }
        //随机生成UUID
        String uuid = UUID.randomUUID().toString().replace("-","");
        System.out.println(uuid);
        redisTemplate.boundValueOps(uuid).set(username);
        //设置过期时间
        redisTemplate.boundValueOps(uuid).expire(30, TimeUnit.MINUTES);
        return Result.build(ConstantEnum.SUCCESS).setResult("登陆成功").setResult(uuid);
    }


    @ApiOperation(value = "学生注册功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student",value = "学生实体类",required = true,paramType = "body")
    })
    @PostMapping("register")
    public Result register(@RequestBody TbStudent student){
        //盐
        String salt = ShiroUtils.generateSalt(6);
        //加密过后的密码
        String md5 = ShiroUtils.encryptPassword("MD5", student.getStudentPassword(), salt, 3);
        student.setStudentPassword(md5);
        student.setStudentSalt(salt);
        int i = tbStudentMapper.insert(student);
        if (i > 0 ){
            return Result.build(ConstantEnum.ZHUCE_SUCCESS);
        }
        return Result.build(ConstantEnum.REGISTRATION_FAILED);
    }
}

